local limitInfo=redis.call("HMGET",KEYS[1],"current","maxLimitBucket","onceLimitBucketIncr","limitBucketIncrInterval","lastTime")
local current=tonumber(limitInfo[1])
local maxLimitBucket=tonumber(limitInfo[2])
local onceLimitBucketIncr=tonumber(limitInfo[3])
local limitBucketIncrInterval=tonumber(limitInfo[4])
local lastTime=tostring(limitInfo[5])
local nowTime=tostring(ARGV[1])
if current>0 then
     return redis.call("HINCRBY",KEYS[1],"current",-1)
else
     current=math.min(maxLimitBucket,math.floor((nowTime-lastTime)/limitBucketIncrInterval)*onceLimitBucketIncr+current)
     if current>0 then
          current=current-1
          redis.call("HSET",KEYS[1],"lastTime",nowTime)
          redis.call("HSET",KEYS[1],"current",current)
          return current
     end
end
return -1